package com.treasure.web.configure;

import com.treasure.web.interceptor.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author sunhongmin
 * @date 2023/9/1 23:37
 * @description 登录拦截器
 */
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
    @Resource
    private AuthInterceptor authInterceptor;
    @Resource
    private TraceIdInterceptor traceIdInterceptor;
    @Resource
    private CorpManagementAuthInterceptor corpManagementAuthInterceptor;
    @Resource
    private SystemManagementAuthInterceptor systemManagementAuthInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("file/**")// 静态文件
                .excludePathPatterns("/treasure/auth/register") // 注册
                .excludePathPatterns("/treasure/auth/login") // 登录
                .excludePathPatterns("/treasure/auth/external/login") // 跳转登录
                .excludePathPatterns("/treasure/auth/corp/create") // 企业开通试用
                .excludePathPatterns("/treasure/auth/corp/trial/approve") // 企业试用申请
                .excludePathPatterns("/treasure/auth/logout") // 登出
                .excludePathPatterns("/treasure/auth/edit/password") // 修改密码
                .excludePathPatterns("/treasure/auth/edit/password/code") // 修改密码
                .excludePathPatterns("/error") // 错误页面
                .excludePathPatterns("/treasure/test/**") // 测试接口

                .excludePathPatterns("/treasure/auth/verify/code/send") // 发送验证码
                .excludePathPatterns("/treasure/auth/email/verify/code/send") // 发送邮箱验证码

                .excludePathPatterns("/treasure/sys/admin/login") // 系统后台登录
                .excludePathPatterns("/treasure/sys/admin/logout") // 系统后台登出

                .excludePathPatterns("/treasure/corp/admin/login") // 企业后台登录
                .excludePathPatterns("/treasure/corp/admin/logout") // 企业后台登出

                .excludePathPatterns("/static/**") // 静态资源
                .excludePathPatterns("css/**", "js/**", "img/**", "json/**", "fonts/**", "/*.html") // 静态资源
        ;

        // 链路追踪拦截器
        registry.addInterceptor(traceIdInterceptor)
                .addPathPatterns("/**")
        ;

        // 限流
        registry.addInterceptor(new RateLimitInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/treasure/corp/manage/conversation/list")

                .excludePathPatterns("/static/**") // 静态资源
                .excludePathPatterns("css/**", "js/**", "img/**", "json/**", "fonts/**", "/*.html") // 静态资源
        ;


        // 企业后台登录拦截器
        registry.addInterceptor(corpManagementAuthInterceptor)
                .addPathPatterns("/treasure/corp/admin/**") // 企业后台管理
                .addPathPatterns("/treasure/knowledge/**") // 知识库管理
                .addPathPatterns("/treasure/corp/account/**") // 账户管理
                .excludePathPatterns("/treasure/corp/admin/login") // 企业后台登录
                .excludePathPatterns("/treasure/corp/admin/logout") // 企业后台登出
        ;

        // 超级管理员权限校验拦截器
        registry.addInterceptor(systemManagementAuthInterceptor)
                .addPathPatterns("/treasure/sys/admin/**") // 系统后台管理
                .excludePathPatterns("/treasure/sys/admin/login") // 系统后台登录
                .excludePathPatterns("/treasure/sys/admin/logout") // 系统后台登出
        ;

    }

}
